const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    isCard:true,
    commounty_id:'',
    is_login:false,
    showModalStatus:false,
    showModalStatus_share:false,
    info:[], //详情
    comment_cont:'', //评论内容
    focus:false,
    comment_list:[],
    click_num:0,
    zan_num:0,
    ping_num:0,
    emojiChar: "☺-😋-😌-😍-😏-😜-😝-😞-😔-😪-😭-😁-😂-😃-😅-😆-👿-😒-😓-😔-😏-😖-😘-😚-😒-😡-😢-😣-😤-😢-😨-😳-😵-😷-😸-😻-😼-😽-😾-😿-🙊-🙋-🙏-✈-🚇-🚃-🚌-🍄-🍅-🍆-🍇-🍈-🍉-🍑-🍒-🍓-🐔-🐶-🐷-👦-👧-👱-👩-👰-👨-👲-👳-💃-💄-💅-💆-💇-🌹-💑-💓-💘-🚲",
    //0x1f---
    emoji: [
      "60a", "60b", "60c", "60d", "60f",
      "61b", "61d", "61e", "61f",
      "62a", "62c", "62e",
      "602", "603", "605", "606", "608",
      "612", "613", "614", "615", "616", "618", "619", "620", "621", "623", "624", "625", "627", "629", "633", "635", "637",
      "63a", "63b", "63c", "63d", "63e", "63f",
      "64a", "64b", "64f", "681",
      "68a", "68b", "68c",
      "344", "345", "346", "347", "348", "349", "351", "352", "353",
      "414", "415", "416",
      "466", "467", "468", "469", "470", "471", "472", "473",
      "483", "484", "485", "486", "487", "490", "491", "493", "498", "6b4"
    ],
    emojis: [],//qq、微信原始表情
    page:1,
    show_more:false,
    more_msg:'下拉加载更多'
  },

  onLoad:function(options){
    let _this = this,
      id = options.id;
    _this.setData({
      commounty_id:id
    })

    //处理图片
    var em = {}, 
     emChar = _this.data.emojiChar.split("-");
    _this.data.emoji.forEach(function (v, i) {
      em = {
        char: emChar[i],
        emoji: "0x1f" + v
      };
      _this.data.emojis.push(em)
    });
    _this.setData({
      emojis: _this.data.emojis
    })
    _this.get_commounty_info(); //获取详情
    _this.get_comment_list(); //获取评论列表
    _this.getUserInfo(); //检测是否登陆
  },

  //表情选择
  emojiChoose: function (e) {
    //当前输入内容和表情合并
    let _this = this;
    _this.setData({
      comment_cont: _this.data.comment_cont + e.currentTarget.dataset.emoji
    })
  },

    //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true,
      showModalStatus_share:true,
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 300)
  },

  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false,
        showModalStatus_share: false,
      })
    }.bind(this), 200)
  },


  //提交评论
  sub_comment:function(){
    let _this = this,
      commounty_id = _this.data.commounty_id,
      comment_cont = _this.data.comment_cont,
      is_login = _this.data.is_login;

    if(!is_login){
      wx.showModal({
        title: '评论失败',
        content: '只有登录后才可以评论哦~',
        confirmText:'前往登陆',
        cancelText:'暂不登陆',
        success (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/auth/auth',
            })
          }
        }
      })
      return false;
    }
    if(comment_cont == ''){
      app.showError('评论内容不能为空');
      return false;
    }
    app._post('api/member/sub_comment', {commounty_id:commounty_id,comment_cont:comment_cont,par_id:0}, function(result) {
      if(result.code == 200){
        _this.get_comment_list();
        _this.setData({
          ping_num:_this.data.ping_num*1+1,
          comment_cont:'',
          showModalStatus: false,
          showModalStatus_share: false,
        })
        app.showSuccess(result.msg);
      }else{
        app.showError(result.msg);
      }
    });
  },

  // 页面上拉触底事件的处理函数
  onReachBottom: function() {
    let _this = this;
    if (_this.data.show_more) {
      _this.setData({
        show_more: false
      })
      _this.get_comment_list();
    }
  },

  //输入文字
  input_msg:function(e){
    let _this = this;
    _this.setData({
      comment_cont:e.detail.value
    })
  },

  //获取内容详情
  get_commounty_info:function(){
    let _this = this,
      commounty_id = _this.data.commounty_id;
    app._post('api/index/get_commounty_info', {commounty_id:commounty_id,from:'detail'}, function(result) {
      _this.setData({
        info:result.data,
        click_num:result.data.click_num,
        zan_num:result.data.zan_num,
        ping_num:result.data.ping_num
      })
    });
  },

  //获取个人信息
  getUserInfo:function(){
    let _this = this;
    app._post('api/member/getUserInfo', {}, function(result) {
      if(result.code == 200){
        _this.setData({
          is_login:true,
        })
        _this.check_follow(); //检查是否已关注
      }else{
        _this.setData({
          is_login:false,
        })
      }
    });
  },

  //检查是否已关注
  check_follow:function(){
    
  },

  //获取评论内容
  get_comment_list:function(){
    let _this = this,
    commounty_id = _this.data.commounty_id;
    wx.showLoading({
      title: '玩命加载中',
      show_text: '数据加载中'
    })
    app._post('api/index/get_comment_list', {page:_this.data.page,commounty_id:commounty_id}, function(result) {
      if(result.data.length>0){
        _this.setData({
          comment_list:_this.data.comment_list.concat(result.data),
          page:_this.data.page*1+1,
          show_more:true,
          more_msg:'下拉加载更多'
        })
      }else{
        _this.setData({
          show_more:false,
          more_msg:'沒有更多了',
        })
      }
      wx.hideLoading();
    });
  },

  //动态点赞
  comment_zan:function(){
    let _this = this,
    is_login = _this.data.is_login,
    commounty_id = _this.data.commounty_id;
    if(!is_login){
      wx.showModal({
        title: '点赞失败',
        content: '只有登陆才可以点赞哦~',
        confirmText:'前往登陆',
        cancelText:'暂不登陆',
        success (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/auth/auth',
            })
          }
        }
      })
      return false;
    }

    //执行点赞
    app._post('api/member/comment_zan', {commounty_id:commounty_id}, function(result) {
      if(result.code == 200){
        _this.setData({
          zan_num:_this.data.zan_num*1+1
        })
        app.showSuccess(result.msg);
      }else{
        app.showError(result.msg);
      }
    });
  },
});
